const express = require('express')
const router = express.Router()
const path = require('path')
const article = require('../modles/article')
const commend = require('../modles/commend')

router.get('/', (req, res) => {
    res.sendFile(path.resolve('./static/html/write.html'))
})
//发送全部文章
router.get('/getAllArticle', (req, res) => {
    article.find({}).then((data) => {
        res.send(data)
    }).catch((err) => {
        res.send(err)
    })
})
//删除文章
router.post('/deleteArticle', (req, res) => {
    var { id } = req.body
    article.deleteOne({ _id: id }).then((data) => {
        commend.deleteMany({ fromId: id }).then(() => {
            res.send('删除成功')
        })
    }).catch((err) => {
        res.send('删除失败', err)
    })
})
//添加文章
router.post('/addArticle', (req, res) => {
    var { content, title, classify } = req.body
    var c = new article({
        title: title,
        content: content,
        classify: classify,
        time: new Date().toLocaleString(),
        count: 0
    })
    c.save().then((data) => {
        res.send('文章发布成功！')
    }).catch((err) => {
        res.send('文章发布失败')
    })
})
module.exports = router;